const Goods = require('../model/goods.model')
class GoodsService {
  async createGoods(goods) {
    const res = await Goods.create(goods)
    return res.dataValues
  }

  async updateGoods(goods) {
    const { id, ...resgoods } = goods
    const res = await Goods.update(resgoods, { where: { id } })

    console.log('res:'+res)
    return res[0] > 0 ? true : false
  }

  async removeGoods(id) {
    
    const res = await Goods.destroy({ where: { id } })
    console.log('res:'+res)
    return res > 0 ? true : false
  }

  async restore(id) {
    const res = await Goods.restore({where:{id}})
    console.log(res)
    return res > 0 ? true : false
  }
  
  async findAll(){
    const res = await Goods.findAll()
    return res
  }

  async findGoods(pageNum,pageSize){
    //1.获取总数
    //2.获取分页的具体数据
    // const count = await Goods.count()
    const offset = (pageNum - 1) * pageSize
    // const res = await Goods.findAll({ offset: offset, limit: pageSize * 1}) 

    const {count,rows:res} = await Goods.findAndCountAll({
      offset: offset, limit: pageSize * 1
    })

    return {
      pageNum,
      pageSize,
      total:count,
      list:res
    }
  }

}

module.exports = new GoodsService()
